package com.link2room.aeriabots.controller.api;

import com.link2room.aeriabots.dto.param.base.WxComponentParamDto;
import com.link2room.aeriabots.facade.IWXComponentFacadeService;
import com.link2room.aeriabots.feign.mirage.MemberLoginFeignService;
import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.common.dto.result.base.MemberSessionDto;
import com.link2room.mirage.dto.param.WxLoginParamDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by LN on 2016/9/7.
 */
@Api(tags ={"微信网页授权"} )
@RestController
@RequestMapping("/api/WxOpenid")
public class WxWebAuthenticationOpenidApiController {
    @Resource
    private IWXComponentFacadeService wxComponentFacadeService;
    @Autowired
    private MemberLoginFeignService memberLoginFeignService;

    @ApiOperation(value="微信公众号授权登录",response = MemberSessionDto .class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "授权code",required = true,value = "asdasdasdasd", dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "酒店code",required = false,defaultValue = "L2R", dataType = "String",paramType="query")
    })
    @RequestMapping(method = RequestMethod.GET)
    public BaseResultDto requestForOpenid(
            @RequestParam(name = "accessCode",required = true)String accessCode,
            @RequestParam(name = "lhotelCode",required = false)String lhotelCode
    ) throws Exception {

        WxComponentParamDto wxComponentApiParamDto = new WxComponentParamDto();
        wxComponentApiParamDto.setAccessCode(accessCode);
        wxComponentApiParamDto.setLhotelCode(lhotelCode);
        String openid = wxComponentFacadeService.getOpenid(wxComponentApiParamDto);
        WxLoginParamDto rpd = new WxLoginParamDto();
        rpd.setOpenid(openid);
        //会员登录检测
        MemberSessionDto memberSessionDto = memberLoginFeignService.wxLogin(rpd).getResultInfo();
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        resultDto.setResultInfo(memberSessionDto);
        return resultDto;
    }
}
